/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.drools.rule;

import org.kie.api.builder.KieScanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.qloudgen.sec.util.ExceptionUtil;

/**
 * <p>Title: Drools Implementation</p>
 * <p>Description: Wrappered jboss drools rules engine for maven project and using curator scanner implementation.</p>
 * <p>Copyright: qloudgen/rul 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public final class DroolsCuratorRuleRunner extends AbstractDroolsMavenRuleRunner implements DroolsRuleReceiver {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( DroolsCuratorRuleRunner.class );
	/**
	 *
	 */
	private boolean versionType = true;
	
	/**
	 * Default constructor.
	 */
	public DroolsCuratorRuleRunner() {
	}
	
	/**
	 * Initialize current scanner.
	 *
	 * @param scanner
	 */
	protected void initInternal(KieScanner scanner) {
		logger.info( "Starting initialize drools curator repository scanner...{}" , scanner );
		/**
		 * When using LATEST or RELEASE do need force to scan now.
		 */
		scanner.scanNow();
	}
	
	/**
	 * Destroy current scanner.
	 *
	 * @param scanner
	 */
	protected void destroyInternal(KieScanner scanner) {
		logger.info( "Starting destroy drools curator repository scanner...{}" , scanner );
		/**
		 * Nothing need to do cause never start scanner.
		 */
		//scanner.stop();
	}
	
	/**
	 * Sets current version type.
	 *
	 * @param versionType
	 */
	public void setVersionType(final boolean versionType) {
		this.versionType = versionType;
	}
	
	/**
	 * Returns current version type.
	 *
	 * @return boolean
	 */
	public boolean getVersionType() {
		return versionType;
	}
	
	/**
	 * Receive repository update message.
	 *
	 * @param groupId
	 * @param artifactId
	 * @param version
	 */
	public void receive(String groupId , String artifactId , String version) {
		//
		logger.trace( "Current received release id is...{},{},{}" , groupId , artifactId , version );
		try {
			KieScanner scanner = getScanner( versionType ? new DroolsReleaseId( groupId , artifactId , DroolsReleaseId.VERSION_LATEST ) : new DroolsReleaseId( groupId , artifactId , DroolsReleaseId.VERSION_RELEASE ) );
			logger.info( "Current drools curator repository scanner is...{}" , scanner );
			/**
			 * When using LATEST or RELEASE do need force to scan now.
			 */
			if ( scanner != null ) {
				scanner.scanNow();
			}
		}
		catch (Throwable t) {
			logger.error( "Current drools curator repository scanner error...\n{}" , ExceptionUtil.get( t ) );
			throw new DroolsException( t );
		}
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "]" );
		//
		return builder.toString();
	}
}